package com.more.offer.threadtest.reentrantlocktest;

import java.util.ArrayList;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author pinoc
 * @date 2021/6/26
 * @description 并发复习使用reentLock 实现一个线程安全的list
 */
public class ReentrantLockList {

	private ArrayList<String> array = new ArrayList<>();

	private volatile ReentrantLock lock = new ReentrantLock();

	public void add(String e) {
		lock.lock();
		try{
			array.add(e);
		}  finally {
			lock.unlock();
		}
	}

	public void remove(String e) {
		lock.lock();
		try {
			array.remove(e);
		} finally {
			lock.unlock();
		}
	}

	public String get(int index) {
		lock.lock();
		try {
			return array.get(index);
		} finally {
			lock.unlock();
		}
	}
}
